import {random} from 'lodash'
import fileIconMap from '@/assets/fileIconMaps.json'
import iconfont from '@/assets/icon-file2/iconfont.json'

const availableIcons = iconfont.glyphs.map((f) => f.font_class)

async function wait(time) {
    return new Promise((r) => setTimeout(r, time || random(500, 3000)))
}


function getIconName(ext) {
    // eslint-disable-next-line no-param-reassign
    ext = fileIconMap[ext] || ext
    if (!availableIcons.includes(ext)) {
        return '#icon-cs-other'
    }
    return `#icon-cs-${ext}`
}

function getFileExtension(fileName) {
    if (!fileName.includes('.')) {
        return ''
    }
    return fileName.substr(fileName.lastIndexOf('.') + 1).toUpperCase()
}

function getIconStyle(file) {
    if (file.isDirectory) {
        return {
            class: 'iconfont',
            link: '#icon-cs-folder',
        }
    }
    return {
        class: 'iconfont',
        link: getIconName(getFileExtension(file.fileName))
    }
}

export default {
    wait, getIconStyle
}